package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.aop.ann.LogAnn;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 *  @author SHCJ
 *  @created 2025/9/29 10:43
 *  @description
 */
@SuppressWarnings("All")
@RestController
@Slf4j
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @LogAnn
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门:{}",dept);
        deptService.addDept(dept);
        return Result.success() ;
    }


    @GetMapping
    public Result page(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status
    ) {
        log.info("分页查询,参数:{},{},{},{}",page,pageSize,name,status);
        PageResult<Dept> pageResult = deptService.page(page, pageSize, name, status);
        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询部门id:{}",id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @LogAnn
    @PutMapping()
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }
    @LogAnn
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门id:{}",id);
        deptService.delete(id);
        return Result.success();
    }

//    查询全部
    @GetMapping("list")
    public Result selectAll() {
        log.info("查询全部部门");
        List<Dept> list = deptService.selectAll();
        return Result.success(list);
    }

}
